<?php
class SqliteModel extends Model{
	
	private $audience_list_cows="a.`audience_id`,a.`audience_tel`,a.`audience_email`,a.`audience_name`,a.`audience_position`,a.`audience_portrait`,b.`department_name` as `audience_department`,a.`audience_imUsername` ,a.`audience_imPassword`";
		
	//audience sqlite
	function get_audience_for_sqlite(){
		$sql = 'select '.$this->audience_list_cows.' from audience as a';	
		$sql.= ' join audience_department as b on a.audience_department=b.department_id';
		$sql.= ' where audience_state!=0';
		$sql.= ' and time_insert>=1 and time_delete < time_insert';
		$rs=M('audience','','DB_MEETING')->query($sql);
		
		$sql='';
		if($rs){
			foreach($rs as $val){
				$sql.="insert into audience(`audience_id`,`audience_name`,`audience_tel`,`audience_email`,`audience_department`,`audience_position`,`audience_portrait`,`audience_imUsername`,`audience_imPassword`,`audience_department`)";
				$sql.="values('{$val['audience_id']}','{$val['audience_name']}','{$val['audience_tel']}','{$val['audience_email']}','{$val['audience_department']}','{$val['audience_position']}','{$val['audience_portrait']}','{$val['audience_imUsername']}','{$val['audience_imPassword']}','{$val['audience_department']}');";
			}
		}
		return $sql;
	}
	
	//sqlite
	function get_database_for_sqlite($table,$fields,$field,$last){
		$sql['insert']="select {$fields} from `{$table}` where time_insert>=$last and time_delete < time_insert";
		$add_list 	= M("","","DB_MEETING")->query($sql['insert']);
		if($add_list==null || count($add_list)==0)
			$sql='';
		else{
			$sql='';
			foreach($add_list as $val){
				switch($table){
					case 'audience':
						$sql.="insert into agenda(`audience_id`, `audience_username`, `audience_pwd`, `audience_name`, `audience_tel`, `audience_email`, `audience_department`, `audience_position`, `audience_portrait`, `audience_state`, `audience_nick`, `audience_imUsername`, `audience_imPassword`, `leader_id`)";
						$sql.="values('{$val['agendaId']}','{$val['title']}','{$val['subjectId']}','{$val['tag']}'";
						$sql.=",'{$val['abstraction']}','{$val['location']}','".trim($val['startDate'])."','{$val['startTime']}'";
						$sql.=",'".trim($val['endDate'])."','".trim($val['endTime'])."','{$val['speakerId']}','{$val['groupId']}'";
						$sql.=",'{$val['votes']}','{$val['before_class']}','{$val['end_class']}','{$val['category']}','cn','{$val['exam']}');";	
						break;
				}
			}
		}
		return $sql;
	}
	
	public function save_sqlite_database($path,$category){
		//删除原来的
		$rs=M('sqlite','','DB_MEETING')->where('sqlite_category='.$category)->select();
		if(count($rs) >= 3){
			$rs1=M('sqlite','','DB_MEETING')->where('sqlite_category='.$category)->order('sqlite_id')->find();
			unlink($rs1['sqlite_path']);
			M('sqlite','','DB_MEETING')->delete($rs1['sqlite_id']);
		}
		$data['sqlite_path']=$path;
		$data['sqlite_category']=$category;
		$data['time_insert']=time();
		return M('sqlite','','DB_MEETING')->add($data);	
	}
}
?>